package cn.edu.xjtu.nbrash.xbarsuite;

import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.edu.xjtu.nbrash.xbarsuite.capture.CaptureActivity;
import cn.edu.xjtu.nbrash.xbarsuite.encode.RashEncodeActivity;

/**
 * RashMainActivity.java
 *
 * @author Jingkai Tang jingkaitang@gmail.com
 * @version 0.0.1 Nov 18, 2014 7:00:16 PM
 *
 */

@SuppressWarnings("deprecation")
public class RashMainActivity extends ActivityGroup {

	private TabHost tabHost;
	private LinearLayout tabCapture, tabEncode, tabMore;

	private ImageView capImg, encImg, morImg;
	private TextView capTxt, encTxt, morTxt;

	private static final String RASH_GREEN = "#66FF33";
	private static final String RASH_GRAY = "#666666";

	private boolean notFun;

	private GestureDetector gestureDetector;
	private CaptureActivity captureActivity = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rash_main);
		notFun = false;
		tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup(this.getLocalActivityManager());

		final TabWidget tabWidget = tabHost.getTabWidget();
		tabWidget.setStripEnabled(false);

		tabHost.addTab(tabHost.newTabSpec("Capture").setIndicator("Capture")
				.setContent(new Intent(this, CaptureActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("Encode").setIndicator("Encode")
				.setContent(new Intent(this, RashEncodeActivity.class)));
		tabHost.addTab(tabHost.newTabSpec("More").setIndicator("More")
				.setContent(new Intent(this, RashMoreActivity.class)));

		tabCapture = (LinearLayout) findViewById(R.id.tab_capture);
		tabEncode = (LinearLayout) findViewById(R.id.tab_encode);
		tabMore = (LinearLayout) findViewById(R.id.tab_more);

		capImg = (ImageView) findViewById(R.id.tab_capture_img);
		encImg = (ImageView) findViewById(R.id.tab_encode_img);
		morImg = (ImageView) findViewById(R.id.tab_more_img);

		capTxt = (TextView) findViewById(R.id.tab_capture_txt);
		encTxt = (TextView) findViewById(R.id.tab_encode_txt);
		morTxt = (TextView) findViewById(R.id.tab_more_txt);

		capImg.setBackgroundResource(R.drawable.capture_press);
		encImg.setBackgroundResource(R.drawable.encode);
		morImg.setBackgroundResource(R.drawable.more);

		capTxt.setTextColor(Color.parseColor(RASH_GREEN));
		encTxt.setTextColor(Color.parseColor(RASH_GRAY));
		morTxt.setTextColor(Color.parseColor(RASH_GRAY));

		tabHost.setCurrentTab(0);

		captureActivity = (CaptureActivity) this.getCurrentActivity();

		tabCapture.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setTabCapture();
			}
		});

		tabEncode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setTabEncode();
			}
		});

		tabMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setTabMore();
			}
		});

		gestureDetector = new GestureDetector(this, new OnGestureListener() {

			@Override
			public boolean onSingleTapUp(MotionEvent e) {
				return false;
			}

			@Override
			public void onShowPress(MotionEvent e) {
			}

			@Override
			public boolean onScroll(MotionEvent e1, MotionEvent e2,
					float distanceX, float distanceY) {
				return false;
			}

			@Override
			public void onLongPress(MotionEvent e) {

			}

			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
				if (e1.getX() - e2.getX() > 20
						&& Math.abs(velocityX) > Math.abs(velocityY)) {
					// Fling to left
					tabHost.getCurrentView().startAnimation(
							AnimationUtils.loadAnimation(RashMainActivity.this,
									R.animator.push_left_out));
					setTab((tabHost.getCurrentTab() + 1)
							% tabWidget.getTabCount());
					tabHost.getCurrentView().startAnimation(
							AnimationUtils.loadAnimation(RashMainActivity.this,
									R.animator.push_left_in));
				} else if (e2.getX() - e1.getX() > 20
						&& Math.abs(velocityX) > Math.abs(velocityY)) {
					// Fling to right
					tabHost.getCurrentView().startAnimation(
							AnimationUtils.loadAnimation(RashMainActivity.this,
									R.animator.push_right_out));
					setTab((tabHost.getCurrentTab() + tabWidget.getTabCount() - 1)
							% tabWidget.getTabCount());
					tabHost.getCurrentView().startAnimation(
							AnimationUtils.loadAnimation(RashMainActivity.this,
									R.animator.push_right_in));
				}

				return false;
			}

			@Override
			public boolean onDown(MotionEvent e) {
				return false;
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (captureActivity != null && tabHost.getCurrentTab() == 0 && notFun) {
			// captureActivity.doResume();
			notFun = false;
		}
	}

	@Override
	protected void onPause() {

		if (captureActivity != null && tabHost.getCurrentTab() != 0) {
			captureActivity.doPause();
		} else if (tabHost.getCurrentTab() == 0) {
			notFun = true;
		}
		super.onPause();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		gestureDetector.onTouchEvent(ev);
		return super.dispatchTouchEvent(ev);
	}

	private void setTab(int index) {
		if (tabHost.getCurrentTab() == index) {
			return;
		}

		switch (index) {
			case 0:
				setTabCapture();
				break;

			case 1:
				setTabEncode();
				break;

			case 2:
				setTabMore();
				break;

			default:
				break;
		}
	}

	private void setTabCapture() {
		if (tabHost.getCurrentTab() == 0) {
			return;
		}

		capImg.setBackgroundResource(R.drawable.capture_press);
		encImg.setBackgroundResource(R.drawable.encode);
		morImg.setBackgroundResource(R.drawable.more);
		capTxt.setTextColor(Color.parseColor(RASH_GREEN));
		encTxt.setTextColor(Color.parseColor(RASH_GRAY));
		morTxt.setTextColor(Color.parseColor(RASH_GRAY));
		tabHost.setCurrentTab(0);
	}

	private void setTabEncode() {
		if (tabHost.getCurrentTab() == 1) {
			return;
		}
		if (captureActivity != null && tabHost.getCurrentTab() == 0) {
			captureActivity.doPause();
		}
		capImg.setBackgroundResource(R.drawable.capture);
		encImg.setBackgroundResource(R.drawable.encode_press);
		morImg.setBackgroundResource(R.drawable.more);
		capTxt.setTextColor(Color.parseColor(RASH_GRAY));
		encTxt.setTextColor(Color.parseColor(RASH_GREEN));
		morTxt.setTextColor(Color.parseColor(RASH_GRAY));
		tabHost.setCurrentTab(1);
	}

	private void setTabMore() {
		if (tabHost.getCurrentTab() == 2) {
			return;
		}
		if (captureActivity != null && tabHost.getCurrentTab() == 0) {
			captureActivity.doPause();
		}
		capImg.setBackgroundResource(R.drawable.capture);
		encImg.setBackgroundResource(R.drawable.encode);
		morImg.setBackgroundResource(R.drawable.more_press);
		capTxt.setTextColor(Color.parseColor(RASH_GRAY));
		encTxt.setTextColor(Color.parseColor(RASH_GRAY));
		morTxt.setTextColor(Color.parseColor(RASH_GREEN));
		tabHost.setCurrentTab(2);
	}

	// No Menu BUKAIXIN
	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.rash_main, menu);
	// return true;
	// }
	//
	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// // Handle action bar item clicks here. The action bar will
	// // automatically handle clicks on the Home/Up button, so long
	// // as you specify a parent activity in AndroidManifest.xml.
	// int id = item.getItemId();
	// if (id == R.id.action_settings) {
	// return true;
	// }
	// return super.onOptionsItemSelected(item);
	// }

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
		} else if (keyCode == KeyEvent.KEYCODE_HOME
				&& event.getRepeatCount() == 0 && tabHost.getCurrentTab() == 0) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
